<?php

Raise::load('core.ui.RaiseUIControl');
Raise::load('core.reflection.RaiseVariable');
Raise::load('core.text.html.RaiseHtml');

/**
 * RaiseFieldLabel UIControl class
 * A label for form field
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.control.form
 * @since 1.2
 */
class RaiseFieldLabel extends RaiseUIControl{

    /**
     * The text of the label
     * @var string
     */
    private $value;

    /**
     * The field in which this Label is attached to
     * @var RaiseFormField
     */
    private $field;

    /**
     * The CSS class names
     * @var string
     */
    protected $cssClass;


    /**
     * Create new RaiseTextField
     */
    public function __construct() {

    }

    /**
     * Get or set the RaiseFormField associated with this the label
     * @param RaiseFormField $v (optional) If passed in, the new value will be set.
     * @return RaiseFormField
     */
    public function field($f = false){
        if(func_num_args() == 1){
            if(!RaiseVariable::isSubclass($f, 'RaiseFormField') && $f !== null){
                throw new RaiseInvalidArgumentException('RaiseFieldLabel::field() expecting RaiseFormField or NULL as first parameter; ' . RaiseVariable::typeInfo($f) . ' was given instead.');
            }
            if($this->field != null){
                $this->field->label(null);
            }
            if($f != null){
                $f->label($this);
            }
            $this->field = $f;
        }
        return $this->field;
    }

    /**
     * Get or set the text of the label
     * @param string $v (optional) If passed in, the new value will be set.
     * @return string
     */
    public function value($v = false){
        if(func_num_args() == 1){
            $this->value = $v;
        }
        return $this->value;
    }

    /**
     * Get or set the placeholder text to show when the field is empty.
     * If left null or empty, there will be no placeholder text.
     * @param string $c (optional) If passed in, the new lvalue will be set.
     * @return string
     */
    public function cssClass($c = false){
        if(func_num_args() == 1){
            $this->cssClass = $c;
        }
        return $this->cssClass;
    }

    public function output() {
        $tag = RaiseHtml::tag('label', $this->value(), array('for' => $this->field()->clientId()));
        if($this->cssClass()){
            $tag->attributes()->add('class', $this->cssClass());
        }
        return $tag->html();
    }

}
